package hotLeetcode;

import com.sun.source.tree.WhileLoopTree;

import java.util.Arrays;

public class H148Leetcode {

    public static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    public ListNode sortList(ListNode head) {

        ListNode cur = head;
        int size = 0;
        while (cur != null) {
            cur = cur.next;
            size++;

        }
        int[] arr = new int[size];
        cur = head;
        int index = 0;
        while (cur != null) {
            arr[index++] = cur.val;
            cur = cur.next;

        }

        Arrays.sort(arr);
        cur = head;
        int i = 0;
        while (cur != null) {
            cur.val = arr[i++];
            cur = cur.next;
        }

        return head;
    }

    public static void main(String[] args) {
        ListNode head = new ListNode(4, new ListNode(2, new ListNode(1, new ListNode(3, null))));

        H148Leetcode h148Leetcode = new H148Leetcode();
        h148Leetcode.sortList(head);
    }
}
